﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ImPM.Application
{
    public static class EmailAddressParser
    {
        // Methods
        private static bool IsEmailDelimiter(char c)
        {
            return (((((c == '\0') || (c == ',')) || ((c == ';') || (c == '<'))) || ((c == '>') || (c == ' '))) || ((c >= '\t') && (c <= '\r')));
        }

        public static string[] Parse(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return new string[0];
            }
            List<string> list = new List<string>();
            int startIndex = input.Length - 1;
            Hashtable hashtable = new Hashtable(StringComparer.OrdinalIgnoreCase);
            do
            {
                int num2 = input.LastIndexOf("@", startIndex, startIndex + 1, StringComparison.OrdinalIgnoreCase);
                if (((-1 == num2) || (0 > (num2 - 1))) || ((num2 + 1) > startIndex))
                {
                    break;
                }
                int num3 = num2 - 1;
                if ('"' != input[num3])
                {
                    while ((0 < num3) && !IsEmailDelimiter(input[num3]))
                    {
                        num3--;
                    }
                }
                else
                {
                    num3--;
                    while ((0 < num3) && ('"' != input[num3]))
                    {
                        num3--;
                    }
                }
                int num4 = num2 + 1;
                while ((num4 < startIndex) && !IsEmailDelimiter(input[num4]))
                {
                    num4++;
                }
                if (IsEmailDelimiter(input[num3]))
                {
                    num3++;
                }
                if (IsEmailDelimiter(input[num4]))
                {
                    num4--;
                }
                string item = input.Substring(num3, (num4 - num3) + 1);
                if (((3 <= item.Length) && (('"' != item[0]) || ('"' != item[item.Length - 1]))) && (hashtable[item] == null))
                {
                    list.Add(item);
                    hashtable[item] = true;
                }
                startIndex = num3 - 1;
            }
            while (0 <= startIndex);
            return list.ToArray();
        }
    }
}
